package com.example.chat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.chat.domain.dto.LoginFormDTO;
import com.example.chat.domain.dto.RegisterFormDTO;
import com.example.chat.domain.dto.UpdatePwdFormDTO;
import com.example.chat.domain.dto.UserDTO;
import com.example.chat.domain.vo.UserInfoVo;
import com.example.chat.result.Result;
import com.example.chat.service.UserService;
import com.example.chat.utils.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/users")
@CrossOrigin
@Slf4j
@Tag(name = "用户相关接口")
public class UserController {

    @Resource
    private UserService userService;

    @Operation(summary = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm) {
        String s = loginForm.getPhone() == null ? "学号" : "手机号";
        String number = loginForm.getPhone() == null ? loginForm.getStudentId() : loginForm.getPhone();
        log.info("{}为{}的用户登录",s,number,loginForm.getPhone());
        return userService.login(loginForm);
    }

    @Operation(summary = "游客登录接口")
    @PostMapping("/guestLogin")
    public Result guestLogin(@RequestBody UserDTO userDTO) {
        log.info("游客登录");
        return userService.guestLogin(userDTO.getUserName());
    }

    @Operation(summary = "用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterFormDTO registerFormDTO) {
        log.info("手机号为{}的用户注册",registerFormDTO.getPhone());
        return userService.register(registerFormDTO);
    }

    @Operation(summary = "图片上传接口")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file){
        return userService.upload(file);
    }

    @Operation(summary = "获取个人信息接口")
    @GetMapping
    public Result getMyself(){
        return userService.getMyself();
    }

    @Operation(summary = "更改个人信息接口")
    @PutMapping("/update-self")
    public Result updateSelf(@RequestBody UserDTO userDTO){
        return userService.updateSelf(userDTO);
    }

    @Operation(summary = "获取个人完整信息接口")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        return userService.getUserTypeById(id);
    }

    @Operation(summary = "修改密码")
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdFormDTO updatePwdFormDTO){
        log.info("ID为{}的用户修改密码", UserHolder.getCurrentId());
        return userService.updatePwd(
                updatePwdFormDTO.getOldPassword(),
                updatePwdFormDTO.getNewPassword(),
                updatePwdFormDTO.getConfirmPassword());
    }
}
